package com.JapanCustom.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RedisDataService {
    private final RedisTemplate<String, String> redisTemplate;
    private final ObjectMapper objectMapper;

    public RedisDataService(RedisTemplate<String, String> redisTemplate, ObjectMapper objectMapper) {
        this.redisTemplate = redisTemplate;
        this.objectMapper = objectMapper;
    }

    // 获取表1数据（所有日期+国家）
    public List<Map<String, Object>> getTable1Data() throws Exception {
        String json = redisTemplate.opsForValue().get("japan_trade_table1");
        return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {});
    }

    // 获取表2数据（所有日期汇总）
    public List<Map<String, Object>> getTable2Data() throws Exception {
        String json = redisTemplate.opsForValue().get("japan_trade_table2");
        return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {});
    }
}